1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.builder;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  /**
30   * Unit tests {@link org.apache.commons.lang3.builder.NoFieldNamesToStringStyleTest}.
31   *
32   * @version $Id$
33   */
34  public class NoFieldNamesToStringStyleTest {
35  
36      private final Integer base = Integer.valueOf(5);
37      private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
38      
39      @Before
40      public void setUp() throws Exception {
41          ToStringBuilder.setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE);
42      }
43  
44      @After
45      public void tearDown() throws Exception {
46          ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
47      }
48  
49      //----------------------------------------------------------------
50      
51      @Test
52      public void testBlank() {
53          assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
54      }
55  
56      @Test
57      public void testAppendSuper() {
58          assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
59          assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
60          
61          assertEquals(baseStr + "[hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString());
62          assertEquals(baseStr + "[<null>,hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString());
63          assertEquals(baseStr + "[hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
64      }
65      
66      @Test
67      public void testObject() {
68          final Integer i3 = Integer.valueOf(3);
69          final Integer i4 = Integer.valueOf(4);
70          assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) null).toString());
71          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
72          assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append("a", (Object) null).toString());
73          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append("a", i3).toString());
74          assertEquals(baseStr + "[3,4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
75          assertEquals(baseStr + "[<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString());
76          assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), false).toString());
77          assertEquals(baseStr + "[[]]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), true).toString());
78          assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), false).toString());
79          assertEquals(baseStr + "[{}]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), true).toString());
80          assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", (Object) new String[0], false).toString());
81          assertEquals(baseStr + "[{}]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString());
82      }
83  
84      @Test
85      public void testPerson() {
86          final Person p = new Person();
87          p.name = "Ron Paul";
88          p.age = 72;
89          p.smoker = false;
90          final String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p));
91          assertEquals(pBaseStr + "[Ron Paul,72,false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString());
92      }
93  
94      @Test
95      public void testLong() {
96          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString());
97          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append("a", 3L).toString());
98          assertEquals(baseStr + "[3,4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString());
99      }
100 
101     @Test
102     public void testObjectArray() {
103         Object[] array = new Object[] {null, base, new int[] {3, 6}};
104         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString());
105         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object) array).toString());
106         array = null;
107         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
108         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
109     }
110 
111     @Test
112     public void testLongArray() {
113         long[] array = new long[] {1, 2, -3, 4};
114         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
115         assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object) array).toString());
116         array = null;
117         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
118         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
119     }
120 
121     @Test
122     public void testLongArrayArray() {
123         long[][] array = new long[][] {{1, 2}, null, {5}};
124         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
125         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object) array).toString());
126         array = null;
127         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
128         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
129     }
130 
131 }